# from tools import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import tqdm
import random
from extract_center import CenterExtracter
images_dir = "../data/images1"
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
def get_center(
image_path, crop=None, h=100, skip_x=None, skip_y=None, contrast=110, plot=True
):
"""
Determines the coordinates of center and the radius of the drop
Parameters
----------
image_path : str
Path to the image
crop : tuple with (x,y), optional
Coordinates to crop the image. The default is None.
x: int
x-coordinate of the top-left corner
y: int
y-coordinate of the top-left corner
h : int, optional
Height of the image. The default is 100.
skip_x : int, optional
Number of pixels to skip in x direction. The default is None.
skip_y : int, optional
Number of pixels to skip in y direction. The default is None.
contrast : int, optional
Contrast threshold. The default is 110.
plot : bool, optional
If True, plots the image. The default is True.
Returns
-------
r : int
Radius of the drop
(x,y) : tuple
Coordinates of the center
"""
# Reading image
im = plt.imread(image_path)
# Cropping image
if crop is not None:
crop = [crop[0], crop[1], crop[0] + h, crop[1] + h]
im_c = im[crop[1] : crop[3], crop[0] : crop[2]]
else:
im_c = im
# Using contrast threshold
im_f = (im_c > contrast).astype(int)
skip_pixels_x = skip_x if skip_x is not None else 0
# Along axis 0
xs = im_f.argmin(axis=0)
xss = np.nonzero(xs)
xl = skip_pixels_x + xss[0][0]
xr = skip_pixels_x + xss[0][-1]
# Along axis 1
skip_pixels_y = skip_y if skip_y is not None else 0
ys = im_f.argmin(axis=1)
yss = np.nonzero(ys)
yu = yss[0][0]
yd = yss[0][-1]
# Calculating center and radius
r1 = np.abs((xl - xr)) / 2
r2 = np.abs((yu - yd)) / 2
r = int((r1 + r2) / 2)
x = int(crop[0] + xl + r)
y = int(crop[1] + yu + r)
if plot:
# Plotting the image with center
plt.figure(figsize=(10, 10))
plt.imshow(im, cmap="gray")
plt.hlines(y, 0, im.shape[1], color="g")
plt.vlines(x, 0, im.shape[0], color="b")
plt.annotate(f"{x,y}", xy=(x, y), xytext=(x + 20, y + 30), color="r")
plt.grid()
plt.show()
return r, (x, y)
files = os.listdir(images_dir)
images = [f for f in files if f.endswith(".jpg")]
len(images)
id = [num for num in range(0, len(images))]
img_path = [images_dir+"/" + str(num) + ".jpg" for num in range(0, len(images))]
df = pd.DataFrame({"id": id, "image_path": img_path})
df.head()
# df.to_csv("images.csv", index=False)
def show_image(image_path):
plt.figure(figsize=(10, 10))
plt.imshow(plt.imread(image_path), cmap="gray")
plt.grid()
plt.title(image_path)
plt.show()
coordinates = []
crop = (800, 450)
for f in tqdm.tqdm(df["image_path"], desc="Extracting coordinates..."):
try:
coordinates.append(get_center(f, plot=False, crop=crop))
except:
coordinates.append(None)
df["coordinates"] = coordinates
df.head()
df["coordinates"].notna().sum()
df[df["coordinates"].notna()]
images = df["image_path"]
show_image(images[0])
crop = (800, 395)
samples = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
for sample in samples:
print(sample)
get_center(images[sample], plot=True, crop=crop)
crop = (800, 410)
sample = 50
get_center(images[sample], plot=True, crop=crop)
crop = (800, 450, 1000, 550)
sample = 60
get_center(images[sample], plot=True, crop=crop)
crop = (850, 510)
sample = 65
h=50
get_center(images[sample], plot=True, crop=crop, h=h)
h=50
crop=(950, 570)
sample = 110
get_center(images[sample], plot=True, crop=crop, h=h)
h=100
crop=(1050, 490)
sample = 145
get_center(images[sample], plot=True, crop=crop, h=h)
For starting images, start from (800, 395). Upto 35. Then use (800, 410) upto 50. Use (800, 450) for upto 60. Use crop = (850, 500) and h=50 upto 65. Then collison starts and lasts for 107.
107-110 h=50 crop=(950, 570)
The collison happens from 67 to 107.
show_image(images[150])
show_image(img_path[0])
c = CenterExtracter()
c.X, c.Y
x = 150
y = 40
h = 40
(r1, r2), (xc_p, yc_p) = c.get_center(
img_path[0], x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
centers = {
img_path[0]: (xc_p, yc_p),
}
radii = {
img_path[0]: (r1, r2),
}
for img in img_path[1:70]:
# print(img)
r, (xc, yc) = c.get_center(
img, x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
centers[img] = (xc, yc)
radii[img] = r
x = x + (xc - xc_p)
y = y + (yc - yc_p)
xc_p = xc
yc_p = yc
image_path = list(centers.keys())[-1]
plt.figure(figsize=(10, 10))
plt.imshow(plt.imread(image_path), cmap="gray")
plt.grid()
plt.title(image_path)
plt.hlines(c.Y+centers[image_path][1], 0, 1300, color="r")
plt.vlines(c.X+centers[image_path][0], 0, 800, color="r")
plt.show()
x = 820-c.X
y= 600-c.Y
h=50
r, (xc_p, yc_p) = c.get_center(
img_path[75], x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
# centers = {
# img_path[0]: (xc_p, yc_p),
# }
for img in img_path[76:100]:
# print(img)
r, (xc, yc) = c.get_center(
img, x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
x = x+(xc-xc_p)
y = y+(yc-yc_p)
centers[img] = (xc, yc)
radii[img] = r
xc_p = xc
yc_p = yc
img
image_path = list(centers.keys())[-1]
plt.figure(figsize=(10, 10))
plt.imshow(plt.imread(image_path), cmap="gray")
plt.grid()
plt.title(image_path)
plt.hlines(c.Y+centers[image_path][1], 0, 1300, color="r")
plt.vlines(c.X+centers[image_path][0], 0, 800, color="r")
plt.show()
x = 950-c.X
y= 580-c.Y
h=40
r, (xc_p, yc_p) =c.get_center(
img_path[106], x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
# centers = {
# img_path[0]: (xc_p, yc_p),
# }
for img in img_path[107:-16]:
# print(img)
r, (xc, yc) = c.get_center(
img, x=x, y=y, h=h, w=h, strict=True, plot=False, crop_included=False
)
x = x+(xc-xc_p)
y = y+(yc-yc_p)
centers[img] = (xc, yc)
radii[img] = r
xc_p = xc
yc_p = yc
image_path = list(centers.keys())[-3]
plt.figure(figsize=(10, 10))
plt.imshow(plt.imread(image_path), cmap="gray")
plt.grid()
plt.title(image_path)
plt.hlines(c.Y+centers[image_path][1], 0, 1300, color="r")
plt.vlines(c.X+centers[image_path][0], 0, 800, color="r")
plt.show()
len(centers), len(img_path)
len_samples = 49
plt.figure(figsize=(30, 30))
samples = random.sample(list(centers.keys()), len_samples)
for img in samples:
plt.subplot(7, 7, samples.index(img)+1)
x, y = centers[img]
plt.imshow(plt.imread(img)[200:, 500:], cmap="gray")
plt.hlines(c.Y+y-200, 0, 1300-600, color="r")
plt.vlines(c.X+x-500, 0, 800-300, color="g")
plt.title(img.split("/")[-1])
plt.axis("off")
plt.tight_layout()
plt.savefig("samples_with_dynamic_cropping.jpg")
df = pd.DataFrame(centers).T.reset_index()
df.columns =["image_path", "x", "y"]
df2 = pd.DataFrame(radii).T.reset_index()
df2.columns =["image_path", "r1", "r2"]
df = pd.merge(df, df2, on="image_path")
df["x"] = df["x"]+c.X
df["y"] = df["y"]+c.Y
df.to_csv("../data/results/with_dynamic_cropping.csv", index=False)
plt.plot(df["y"], ".")
plt.title("y with time");
plt.plot(df["x"], ".")
plt.title("x with time");
plt.plot(df["x"], -df["y"], ".")
plt.title("x-y with time");
df["vy"] = df["y"].diff()
df["vx"] = df["x"].diff()
df["v"] = np.sqrt(df["vy"]**2 + df["vx"]**2)
plt.plot(df["v"], ".")
plt.title("v with time");
plt.plot(df["vy"], ".")
plt.title("vy with time");
plt.plot(df["vx"], ".")
plt.title("vx with time");
df.describe()
from run import Run
r = Run("../data/images1")
cs, rs = r.dynamic_cropping(save=True,strict=True, plot=False, crop_included=False)